<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>ActiveModel::Dirty</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <link rel="stylesheet" href="../../css/reset.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/main.css" type="text/css" media="screen" />
<link rel="stylesheet" href="../../css/github.css" type="text/css" media="screen" />
<script src="../../js/jquery-1.3.2.min.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/jquery-effect.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/main.js" type="text/javascript" charset="utf-8"></script>
<script src="../../js/highlight.pack.js" type="text/javascript" charset="utf-8"></script>

</head>

<body>     
    <div class="banner">
        
            <span>Ruby on Rails v4.0.0</span><br />
        
        <h1>
            <span class="type">Module</span> 
            ActiveModel::Dirty 
            
        </h1>
        <ul class="files">
            
            <li><a href="../../files/activemodel/lib/active_model/dirty_rb.html">activemodel/lib/active_model/dirty.rb</a></li>
            
        </ul>
    </div>
    <div id="bodyContent">
        <div id="content">
  
    <div class="description">
      
<h2 id="label-Active+Model+Dirty">Active Model Dirty</h2>

<p>Provides a way to track changes in your object in the same way as Active
Record does.</p>

<p>The requirements for implementing <a
href="Dirty.html">ActiveModel::Dirty</a> are:</p>
<ul><li>
<p><code>include ActiveModel::Dirty</code> in your object.</p>
</li><li>
<p>Call <code>define_attribute_methods</code> passing each method you want to
track.</p>
</li><li>
<p>Call <code>attr_name_will_change!</code> before each change to the tracked
attribute.</p>
</li></ul>

<p>If you wish to also track previous changes on save or update, you need to
add:</p>

<pre>@previously_changed = changes</pre>

<p>inside of your save or update method.</p>

<p>A minimal implementation could be:</p>

<pre class="ruby"><span class="ruby-keyword">class</span> <span class="ruby-constant">Person</span>
  <span class="ruby-identifier">include</span> <span class="ruby-constant">ActiveModel</span><span class="ruby-operator">::</span><span class="ruby-constant">Dirty</span>

  <span class="ruby-identifier">define_attribute_methods</span> :<span class="ruby-identifier">name</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">name</span>
    <span class="ruby-ivar">@name</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">name=</span>(<span class="ruby-identifier">val</span>)
    <span class="ruby-identifier">name_will_change!</span> <span class="ruby-keyword">unless</span> <span class="ruby-identifier">val</span> <span class="ruby-operator">==</span> <span class="ruby-ivar">@name</span>
    <span class="ruby-ivar">@name</span> = <span class="ruby-identifier">val</span>
  <span class="ruby-keyword">end</span>

  <span class="ruby-keyword">def</span> <span class="ruby-identifier">save</span>
    <span class="ruby-ivar">@previously_changed</span> = <span class="ruby-identifier">changes</span>
    <span class="ruby-ivar">@changed_attributes</span>.<span class="ruby-identifier">clear</span>
  <span class="ruby-keyword">end</span>
<span class="ruby-keyword">end</span>
</pre>

<p>A newly instantiated object is unchanged:</p>

<pre class="ruby"><span class="ruby-identifier">person</span> = <span class="ruby-constant">Person</span>.<span class="ruby-identifier">find_by</span>(<span class="ruby-identifier">name</span><span class="ruby-operator">:</span> <span class="ruby-string">'Uncle Bob'</span>)
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">changed?</span>       <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>Change the name:</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'Bob'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">changed?</span>       <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name_changed?</span>  <span class="ruby-comment"># =&gt; true</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name_was</span>       <span class="ruby-comment"># =&gt; &quot;Uncle Bob&quot;</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name_change</span>    <span class="ruby-comment"># =&gt; [&quot;Uncle Bob&quot;, &quot;Bob&quot;]</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'Bill'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name_change</span>    <span class="ruby-comment"># =&gt; [&quot;Uncle Bob&quot;, &quot;Bill&quot;]</span>
</pre>

<p>Save the changes:</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">save</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">changed?</span>       <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name_changed?</span>  <span class="ruby-comment"># =&gt; false</span>
</pre>

<p>Assigning the same value leaves the attribute unchanged:</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'Bill'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name_changed?</span>  <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name_change</span>    <span class="ruby-comment"># =&gt; nil</span>
</pre>

<p>Which attributes have changed?</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'Bob'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">changed</span>        <span class="ruby-comment"># =&gt; [&quot;name&quot;]</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">changes</span>        <span class="ruby-comment"># =&gt; {&quot;name&quot; =&gt; [&quot;Bill&quot;, &quot;Bob&quot;]}</span>
</pre>

<p>If an attribute is modified in-place then make use of
<code>[attribute_name]_will_change!</code> to mark that the attribute is
changing. Otherwise <a href="../ActiveModel.html">ActiveModel</a> can’t
track changes to in-place attributes.</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">name_will_change!</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name_change</span>    <span class="ruby-comment"># =&gt; [&quot;Bill&quot;, &quot;Bill&quot;]</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-string">'y'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name_change</span>    <span class="ruby-comment"># =&gt; [&quot;Bill&quot;, &quot;Billy&quot;]</span>
</pre>

    </div>
  


  


  
  


  


  
    <!-- Method ref -->
    <div class="sectiontitle">Methods</div>
    <dl class="methods">
      
        <dt>C</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Dirty.html#method-i-changed">changed</a>,
              </li>
            
              
              <li>
                <a href="Dirty.html#method-i-changed-3F">changed?</a>,
              </li>
            
              
              <li>
                <a href="Dirty.html#method-i-changed_attributes">changed_attributes</a>,
              </li>
            
              
              <li>
                <a href="Dirty.html#method-i-changes">changes</a>
              </li>
            
          </ul>
        </dd>
      
        <dt>P</dt>
        <dd>
          <ul>
            
              
              <li>
                <a href="Dirty.html#method-i-previous_changes">previous_changes</a>
              </li>
            
          </ul>
        </dd>
      
    </dl>
  

  
    <!-- Includes -->
    <div class="sectiontitle">Included Modules</div>
    <ul>
      
        <li>
          
            <a href="AttributeMethods.html">
              ActiveModel::AttributeMethods
            </a>
          
        </li>
      
    </ul>
  



  

    

    

    


    


    <!-- Methods -->
        
      <div class="sectiontitle">Instance Public methods</div>
      
        <div class="method">
          <div class="title method-title" id="method-i-changed">
            
              <b>changed</b>()
            
            <a href="Dirty.html#method-i-changed" name="method-i-changed" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns an array with the name of the attributes with unsaved changes.</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">changed</span> <span class="ruby-comment"># =&gt; []</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'bob'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">changed</span> <span class="ruby-comment"># =&gt; [&quot;name&quot;]</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-changed_source')" id="l_method-i-changed_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/04cda1848cb847c2bdad0bfc12160dc8d5547775/activemodel/lib/active_model/dirty.rb#L111" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-changed_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/dirty.rb, line 111</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">changed</span>
  <span class="ruby-identifier">changed_attributes</span>.<span class="ruby-identifier">keys</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-changed-3F">
            
              <b>changed?</b>()
            
            <a href="Dirty.html#method-i-changed-3F" name="method-i-changed-3F" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns <code>true</code> if any attribute have unsaved changes,
<code>false</code> otherwise.</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">changed?</span> <span class="ruby-comment"># =&gt; false</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'bob'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">changed?</span> <span class="ruby-comment"># =&gt; true</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-changed-3F_source')" id="l_method-i-changed-3F_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/04cda1848cb847c2bdad0bfc12160dc8d5547775/activemodel/lib/active_model/dirty.rb#L102" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-changed-3F_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/dirty.rb, line 102</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">changed?</span>
  <span class="ruby-identifier">changed_attributes</span>.<span class="ruby-identifier">present?</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-changed_attributes">
            
              <b>changed_attributes</b>()
            
            <a href="Dirty.html#method-i-changed_attributes" name="method-i-changed_attributes" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns a hash of the attributes with unsaved changes indicating their
original values like <code>attr =&gt; original value</code>.</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> <span class="ruby-comment"># =&gt; &quot;bob&quot;</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'robert'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">changed_attributes</span> <span class="ruby-comment"># =&gt; {&quot;name&quot; =&gt; &quot;bob&quot;}</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-changed_attributes_source')" id="l_method-i-changed_attributes_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/04cda1848cb847c2bdad0bfc12160dc8d5547775/activemodel/lib/active_model/dirty.rb#L141" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-changed_attributes_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/dirty.rb, line 141</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">changed_attributes</span>
  <span class="ruby-ivar">@changed_attributes</span> <span class="ruby-operator">||=</span> {}
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-changes">
            
              <b>changes</b>()
            
            <a href="Dirty.html#method-i-changes" name="method-i-changes" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns a hash of changed attributes indicating their original and new
values like <code>attr =&gt; [original value, new value]</code>.</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">changes</span> <span class="ruby-comment"># =&gt; {}</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'bob'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">changes</span> <span class="ruby-comment"># =&gt; { &quot;name&quot; =&gt; [&quot;bill&quot;, &quot;bob&quot;] }</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-changes_source')" id="l_method-i-changes_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/04cda1848cb847c2bdad0bfc12160dc8d5547775/activemodel/lib/active_model/dirty.rb#L121" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-changes_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/dirty.rb, line 121</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">changes</span>
  <span class="ruby-constant">ActiveSupport</span><span class="ruby-operator">::</span><span class="ruby-constant">HashWithIndifferentAccess</span>[<span class="ruby-identifier">changed</span>.<span class="ruby-identifier">map</span> { <span class="ruby-operator">|</span><span class="ruby-identifier">attr</span><span class="ruby-operator">|</span> [<span class="ruby-identifier">attr</span>, <span class="ruby-identifier">attribute_change</span>(<span class="ruby-identifier">attr</span>)] }]
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
        
        <div class="method">
          <div class="title method-title" id="method-i-previous_changes">
            
              <b>previous_changes</b>()
            
            <a href="Dirty.html#method-i-previous_changes" name="method-i-previous_changes" class="permalink">Link</a>
          </div>
          
          
            <div class="description">
              <p>Returns a hash of attributes that were changed before the model was saved.</p>

<pre class="ruby"><span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> <span class="ruby-comment"># =&gt; &quot;bob&quot;</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">name</span> = <span class="ruby-string">'robert'</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">save</span>
<span class="ruby-identifier">person</span>.<span class="ruby-identifier">previous_changes</span> <span class="ruby-comment"># =&gt; {&quot;name&quot; =&gt; [&quot;bob&quot;, &quot;robert&quot;]}</span>
</pre>
            </div>
          
          
          
          
          
            
            <div class="sourcecode">
              
              <p class="source-link">
                Source: 
                <a href="javascript:toggleSource('method-i-previous_changes_source')" id="l_method-i-previous_changes_source">show</a>
                
                  | <a href="https://github.com/rails/rails/blob/04cda1848cb847c2bdad0bfc12160dc8d5547775/activemodel/lib/active_model/dirty.rb#L131" target="_blank" class="github_url">on GitHub</a>
                
              </p>
              <div id="method-i-previous_changes_source" class="dyn-source">
                <pre><span class="ruby-comment"># File activemodel/lib/active_model/dirty.rb, line 131</span>
<span class="ruby-keyword">def</span> <span class="ruby-keyword ruby-title">previous_changes</span>
  <span class="ruby-ivar">@previously_changed</span>
<span class="ruby-keyword">end</span></pre>
              </div>
            </div>
            
          </div>
                    </div>

    </div>
  </body>
</html>    